package com.zmn.mcc.dubbo.impl.module;

import com.zmn.common.dto2.ResponseDTO;
import com.zmn.mcc.common.dto.module.ModuleDIO;
import com.zmn.mcc.common.dto.module.ModuleDRO;
import com.zmn.mcc.dubbo.dto.DubboConsts;
import com.zmn.mcc.dubbo.interfaces.module.ModuleListRemoteService;
import com.zmn.mcc.model.entity.module.McModule;
import com.zmn.mcc.model.entity.module.McModuleQuery;
import com.zmn.mcc.services.interfaces.module.ModuleService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.BeanUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author tanghongjun
 * @since 2020年7月3日17:24:17
 */
@Service(version = DubboConsts.INTERFACE_VERSION, validation = "true")
public class ModuleListRemoteServiceImpl implements ModuleListRemoteService {

    @Resource
    ModuleService moduleService;

    @Override
    public ResponseDTO<List<ModuleDRO>> listByQuery(ModuleDIO query) {
        if (Objects.isNull(query)) {
            return ResponseDTO.fail("入参不能为空");
        }

        McModuleQuery mcModuleQuery = new McModuleQuery();
        BeanUtils.copyProperties(query, mcModuleQuery);
        List<McModule> mcModuleList = moduleService.listByQuery(mcModuleQuery);

        List<ModuleDRO> dros = mcModuleList.stream().map(mcModule -> {
            ModuleDRO moduleDRO = new ModuleDRO();
            BeanUtils.copyProperties(mcModule, moduleDRO);
            return moduleDRO;
        }).collect(Collectors.toList());

        return ResponseDTO.success(dros);
    }
}
